<?php
$dp = new jDatePicker();
$dp->__dateFormat = "yy-mm-dd";
echo "<scr"."ipt type=\"text/javascript\">";
$dp->generateDP("tanggal_dp");
echo "</scr"."ipt>";
$dp->init();
?>

<script>

    function save() {        
        var mandatory = [<?=$id_jenis?>,"tanggal_dp"];
        var mandatoryName = ["Jenis Penjualan","Tanggal"];

        for(var i =0; i < mandatory.length; i++){
            if(document.getElementById(mandatory[i]).value == ''){
                alert('Parameter '+mandatoryName[i]+' Penjualan tidak boleh kosong!');
                return false;
            }
        }
        alert('Finalasi kas berhasil, item penjualan telah dimasukkan ke Data Penjualan');
        return true;
    }

    function _obj_fSendValue(kode, nama, elmKode, elmNama) {
        var elmk = document.getElementById("" + elmKode + "");
        var elmn = document.getElementById("" + elmNama + "");
        try {
            elmk.value = kode;
            elmn.value = nama;
        }
        catch(e) {
            alert(e.message);
        }
    }

    function append_table(result){
        //var result = jQuery.parseJSON(array);
        var length = jQuery("#tablebody").find("tr").length;

        if(length == 1){
            jQuery("#no_item").hide();
        }
        var id = length+''+result.item;
        jQuery("#tablebody").append(
        '<tr class="GridItem1">'+
            '<td>'+length+'<input type="hidden" name="id_item[]" value="'+result.item+'" /></td>'+
            '<td><input name="nama_item[]" type=text value="'+result.nama+'" id=nama_item_'+id+' ></td>'+
            '<td><input name="quantity[]" onblur="UpdateTotals('+id+');" type=text value='+result.qty+' id=qty_'+id+'> '+result.satuan+'</td>'+
            '<td class="IDR" align="right"><input name="harga_beli[]" onblur="UpdateTotals('+id+');" type=text value='+result.harga_beli+' id=harga_beli_'+id+'></td>'+
            '<td class="IDR" align="right"><input name="harga_jual[]" onblur="UpdateTotals('+id+');" type=text value='+result.harga_jual+' id=harga_jual_'+id+'></td>'+
            '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=total id=total_'+id+'>'+result.total+'</span></td>'+
            '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=margin id=margin_'+id+'>'+result.margin+'</span></td>'+
            '<td><a class="delete" onclick="remove_element('+id+',this);">&nbsp;</a></td>'+
            '</tr>'
    );

        jQuery("#grandtotal").html(parseFloat(jQuery("#grandtotal").html()) + parseFloat(result.total));
        jQuery("#grandtotalmargin").html(parseFloat(jQuery("#grandtotalmargin").html()) + parseFloat(result.margin));
    }

    function UpdateTotals(id) {
        var qty = jQuery("#qty_"+id).val();
        var harga_jual = jQuery("#harga_jual_"+id).val();
        var harga_beli = jQuery("#harga_beli_"+id).val();        
        jQuery("#total_"+id).html(parseInt(qty) * parseFloat(harga_jual));
        jQuery("#margin_"+id).html(parseInt(qty) * (parseFloat(harga_jual) - parseFloat(harga_beli)));
        CalculateTotal();
    }

    function CalculateTotal(){
        var totals = jQuery('.total');
        var grandTotal = 0;
        jQuery.each(totals, function(i){            
            grandTotal += parseFloat(jQuery(totals[i]).html());
        });        
        jQuery('#grandtotal').html(parseFloat(grandTotal));

        var margins = jQuery('.margin');
        var grandTotalMargin = 0;
        jQuery.each(margins, function(i){
            grandTotalMargin += parseFloat(jQuery(margins[i]).html());
        });
        jQuery('#grandtotalmargin').html(parseFloat(grandTotalMargin));
    }

    function remove_element(id, elm){
        var total_deleted = jQuery('#total_'+id).html();
        var margin_deleted = jQuery('#margin_'+id).html();
        jQuery("#grandtotal").html(parseFloat(jQuery("#grandtotal").html()) - parseFloat(total_deleted));
        jQuery("#grandtotalmargin").html(parseFloat(jQuery("#grandtotalmargin").html()) - parseFloat(margin_deleted));
        var tr_parent = jQuery(elm).parent().parent();
        jQuery(tr_parent).remove();

        var length = jQuery("#tablebody").find("tr").length;

        if(length == 1){
            jQuery("#no_item").show();
        }
    }

    function clear_element(elm){
        jQuery("#nama_"+elm).removeClass("read-only");
        jQuery("#nama_"+elm).attr("disabled", false);
        jQuery("#nama_"+elm).val("");
        jQuery("#id_"+elm).val("");
    }

    function close_gb(){
<?php echo CloseGB(false); // close greybox   ?>
    }

    function close_gb_2(){
<?php echo CloseGB(true); // close greybox   ?>
    }
</script>

<h3 class="title-popup">Form Tambah Quotation</h3>
<form action="<?php echo base_url("index.php/kas/finalisasi_invoice/$kode/$source_kas")?>" method="post" onsubmit="return save()" enctype="multipart/form-data">
    <div style="padding:0 10px 0 10px;">
        <?php if ($error != "") { ?>
        <div class="BoxMessage Warning">Proses Gagal, <br /><?php echo $error; ?></div>
            <?php } ?>

        <div id="content">
            <div class="DivTable">
                <table>
                    <tr>
                    <dt class="mandatory">Tanggal Penjualan</dt>
                    <dd><input class="read-only" type="text" name="tanggal_dp" id="tanggal_dp" value="<?php echo $tanggal_dp;?>" maxlength="10" size="10" disabled="disabled"  /></dd>

                    <dt class="mandatory">Pilih Jenis</dt>
                    <dd>
                        <input class="read-only" type="text" name="<?=$nama_jenis?>" id="<?=$nama_jenis?>" value="" maxlength="40" size="50" disabled="disabled"  />
                        <input type="hidden" name="<?=$id_jenis?>" id="<?=$id_jenis?>" value="" />
                        <?php echo $gbjeniskaspenjualan; ?>

                    </dd>
                    
                    <dt class="mandatory">Keterangan</dt>
                    <dd>
                        <input type="text" name="keterangan" id="keterangan" value="<?=$keterangan?>" maxlength="100" size="100" />
                    </dd>
                    </tr>
                    <tr>
                        <td colspan="7">
                            <br/>
                            <?php echo $gbitem; ?>
                            <br/><br/>

                            <table class="GridTable" style="width: 100%;">
                                <thead>
                                    <tr class="GridHeader">
                                        <td>No.</td>
                                        <td>Nama</td>
                                        <td>Jumlah</td>
                                        <td>Harga Beli</td>
                                        <td>Harga Jual</td>
                                        <td>Total</td>
                                        <td>Margin</td>
                                        <td width="5%">Remove</td>
                                    </tr>
                                </thead>
                                <tbody id="tablebody">
                                    <tr class="GridItem1" id="no_item">
                                        <td colspan="8">Tidak ada data</td>
                                    </tr>
                                    <?php
                                    $total_harga = 0;
                                    $total_margin = 0;
                                    if(is_object($item)) {
                                        if($item->num_rows() > 0) {
                                            $nomor = 1;
                                            foreach($item->result() as $row) {
                                                echo
                                                '<tr class="GridItem1">'.
                                                        '<td>'.$nomor.'<input type="hidden" name="id_item[]" value="'.$row->id_item.'" /></td>'.                                                        
                                                        '<td><input name="nama_item[]" type=text value="'.$row->nama_item.'" id=nama_item_'.$nomor.'></td>'.
                                                        '<td><input name="quantity[]" onblur="UpdateTotals('.$nomor.');" type=text value='.$row->quantity.' id=qty_'.$nomor.'> '.$row->satuan_beli.'</td>'.
                                                        '<td class="IDR" align="right"><input name="harga_beli[]" onblur="UpdateTotals('.$nomor.');" type=text value='.$row->harga_beli.' id=harga_beli_'.$nomor.'></td>'.
                                                        '<td class="IDR" align="right"><input name="harga_jual[]" onblur="UpdateTotals('.$nomor.');" type=text value='.$row->harga_jual.' id=harga_jual_'.$nomor.'></td>'.
                                                        '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=total id=total_'.$nomor.' style="float: right;">'.($row->harga_jual * $row->quantity).'</span></td>'.
                                                        '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=margin id=margin_'.$nomor.' style="float: right;">'.(($row->harga_jual - $row->harga_beli) * $row->quantity).'</span></td>'.
                                                        '<td><a class="delete" onclick="remove_element('.$nomor.',this);">&nbsp;</a></td>'.
                                                        '</tr>';
                                                $total_harga += $row->harga_jual * $row->quantity;
                                                $total_margin += ($row->harga_jual - $row->harga_beli) * $row->quantity;
                                                $nomor++;
                                            }
                                            echo "<script>jQuery(\"#no_item\").hide();</script>";
                                        }
                                    }

                                    if ($diskon_invoice > 0){
                                        echo
                                                '<tr class="GridItem1">'.
                                                        '<td>'.$nomor.'<input type="hidden" name="id_item[]" value="1" /></td>'.
                                                        '<td><input name="nama_item[]" type=text value="diskon" id=nama_item_'.$nomor.' readonly></td>'.
                                                        '<td><input name="quantity[]" onblur="UpdateTotals('.$nomor.');" type=text value=1 id=qty_'.$nomor.' readonly> Rupiah </td>'.
                                                        '<td class="IDR" align="right"><input name="harga_beli[]" onblur="UpdateTotals('.$nomor.');" type=text value='.$diskon_invoice.' id=harga_beli_'.$nomor.'></td>'.
                                                        '<td class="IDR" align="right"><input name="harga_jual[]" type=text value=0 id=harga_jual_'.$nomor.' readonly></td>'.
                                                        '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=total id=total_'.$nomor.'>0</span></td>'.
                                                        '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=margin id=margin_'.$nomor.'>'.(-1 * $diskon_invoice).'</span></td>'.
                                                        '<td><a class="delete" onclick="remove_element('.$nomor.',this);">&nbsp;</a></td>'.
                                                        '</tr>';
                                                $total_harga += 0;
                                                $total_margin += (-1 * $diskon_invoice);
                                                $nomor++;
                                    }
                                    ?>
                                </tbody>
                                <tr class="GridHeader">
                                    <td colspan="5" align="right">Grand Total</td>
                                    <td class="grand_total" align="right"><span style="float: left;">Rp. </span><span id="grandtotal"><?=($total_harga)?></span></td>
                                    <td class="grand_margin" align="right"><span style="float: left;">Rp. </span><span id="grandtotalmargin"><?=($total_margin)?></span></td>
                                    <td>&nbsp;</td>
                                </tr>
                            </table>
                            <br/><br/>
                        </td>
                    </tr>
                </table>


            </div>
        </div>
    </div>
    <div id="box-control-save">
        <input type="hidden" name="vSave" id="vSave" value="1" />
        <input type="hidden" name="kode" id="kode" value="<?php echo $kode ?>" />
        <input type="submit" class="CommandButton" name="btnSimpan" value="Save" />&nbsp;
        <input type="button" class="CommandButton" value="Cancel" onclick="javascript:closeGB_();"/>
    </div>

</form>